package com.ponhu.ea2.stockcenter.provider.service.entity.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ponhu.ea2.stockcenter.provider.constants.CommonConstant;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryChange;
import com.ponhu.ea2.stockcenter.provider.entity.StockInventoryChangeSnapshot;
import com.ponhu.ea2.stockcenter.provider.mapper.StockInventoryChangeMapper;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockInventoryChangeService;
import com.ponhu.ea2.stockcenter.provider.service.entity.StockInventoryChangeSnapshotService;
import com.ponhu.ea2.stockcenter.provider.support.vo.SalesLevelInventoryChangeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class StockInventoryChangeServiceImpl extends ServiceImpl<StockInventoryChangeMapper, StockInventoryChange>
implements StockInventoryChangeService {

    @Autowired
    private StockInventoryChangeSnapshotService stockInventoryChangeSnapshotService;

    @Override
    public void saveSalesLevelInventoryChangeLog(List<SalesLevelInventoryChangeVO> real, List<SalesLevelInventoryChangeVO> unreal) {
        if(CollectionUtils.isNotEmpty(real)){
            List<StockInventoryChange> entityList = real.stream().map(i -> {
                StockInventoryChange inventoryChange = new StockInventoryChange();
                BeanUtils.copyProperties(i, inventoryChange);
                inventoryChange.setGoodsType(CommonConstant.GOODS_TYPE.REAL);
                return inventoryChange;
            }).collect(Collectors.toList());

            this.saveBatch(entityList);

            List<StockInventoryChangeSnapshot> snapshotList = new ArrayList<>();
            for (SalesLevelInventoryChangeVO entityVO: real){
                StockInventoryChangeSnapshot snapshot = new StockInventoryChangeSnapshot();
                snapshot.setStockChangeId(entityList.stream().filter(f -> f.getInventoryCode().equals(entityVO.getInventoryCode())).findFirst().get().getId())
                        .setInventoryId(entityVO.getInventoryId())
                        .setInventoryCode(entityVO.getInventoryCode())
                        .setGoodsId(entityVO.getGoodsId())
                        .setGoodsType(CommonConstant.GOODS_TYPE.REAL)
                        .setSaleStockNum(entityVO.getSaleStockNum())
                        .setUsedStockNum(entityVO.getUsedStockNum());
                snapshotList.add(snapshot);
            }

            stockInventoryChangeSnapshotService.saveBatch(snapshotList);
        }

        if(CollectionUtils.isNotEmpty(unreal)){
            List<StockInventoryChange> entityList = unreal.stream().map(i -> {
                StockInventoryChange inventoryChange = new StockInventoryChange();
                BeanUtils.copyProperties(i, inventoryChange);
                inventoryChange.setGoodsType(CommonConstant.GOODS_TYPE.UNREAL);
                return inventoryChange;
            }).collect(Collectors.toList());

            this.saveBatch(entityList);

            List<StockInventoryChangeSnapshot> snapshotList = new ArrayList<>();
            for (SalesLevelInventoryChangeVO entityVO: unreal){
                StockInventoryChangeSnapshot snapshot = new StockInventoryChangeSnapshot();
                snapshot.setStockChangeId(entityList.stream().filter(f -> f.getInventoryCode().equals(entityVO.getInventoryCode())).findFirst().get().getId())
                        .setInventoryId(entityVO.getInventoryId())
                        .setInventoryCode(entityVO.getInventoryCode())
                        .setGoodsId(entityVO.getGoodsId())
                        .setGoodsType(CommonConstant.GOODS_TYPE.UNREAL)
                        .setSaleStockNum(entityVO.getSaleStockNum());
                snapshotList.add(snapshot);
            }

            stockInventoryChangeSnapshotService.saveBatch(snapshotList);
        }
    }
}




